//
// Created by luoen on 19-7-5.
//

#include <string.h>
#include <malloc.h>
#include "sample_linked_list.h"
#include "android/log.h"
#include "linked_list.h"

jboolean Java_com_algorithm_practice_fragment_LinkedListFragment_isPalindromeString(JNIEnv * env, jobject ctx, jstring src, jbyte type) {
    if (NULL == src) {
        return 0;
    }
    const char * srcStr = (*env)->GetStringUTFChars(env, src, NULL);
    if (NULL == srcStr) {
        return 0;
    }

    jsize len = (*env)->GetStringLength(env, src);
    if (len == 0) {
        return 0;
    }

    if (len == 1) {
        (*env)->ReleaseStringUTFChars(env, src, srcStr);
        return 1;
    }

    char r_type = (char) type;
    linkedList* list = _create_linked_list(r_type);
    if (NULL == list) {
        (*env)->ReleaseStringUTFChars(env, src, srcStr);
        return 0;
    }
    __android_log_print(ANDROID_LOG_INFO, "sample_linked_list", "srcLen=%d,srcStr=%s", (int)len, srcStr);
    for (int i = 0; i < len; i++) {
        char * temp = NULL;
        temp = (char*)malloc(sizeof(*temp));
        if (NULL == temp) {
            continue;
        }
//        stpncpy(temp, srcStr + i, 1);
        (*env)->GetStringUTFRegion(env, src, i, 1, temp);
        __android_log_print(ANDROID_LOG_INFO, "sample_linked_list", "before=%s", temp);
        _insert_data_to_tail(list, temp);
    }
    if (list->type != 0) {
        size_t count = list->length / 2;
        int i = 0;
        listNode* head = list->head;
        listNode* tail = list->tail;
        for ( ; i < count; ++i) {
            char * head_data = head->data;
            char * tail_data = tail->data;
            if ((* head_data) != (* tail_data)) {
                _release_linked_list(list);
                (*env)->ReleaseStringUTFChars(env, src, srcStr);
                return 0;
            }
            head = head->next;
            tail = tail->pre;
        }
        _release_linked_list(list);
        (*env)->ReleaseStringUTFChars(env, src, srcStr);
        return 1;
    }
    _reverse_linked_list(list);
    listNode * head = list->head;
    int j = 0;
    char * src_index = NULL;
    src_index = (char*)malloc(sizeof(*src_index));
    if (NULL == src_index) {
        (*env)->ReleaseStringUTFChars(env, src, srcStr);
        return 0;
    }
    while (NULL != head) {
        if (j >= len) {
            free(src_index);
            _release_linked_list(list);
            (*env)->ReleaseStringUTFChars(env, src, srcStr);
            return 0;
        }
        char * data = head->data;
        (*env)->GetStringUTFRegion(env, src, j, 1, src_index);
        if ((* src_index) != (* data)) {
            free(src_index);
            _release_linked_list(list);
            (*env)->ReleaseStringUTFChars(env, src, srcStr);
            return 0;
        }
        __android_log_print(ANDROID_LOG_INFO, "sample_linked_list", "after=%s", data);
        head = head->next;
        j += 1;
    }
    free(src_index);
    _release_linked_list(list);
    (*env)->ReleaseStringUTFChars(env, src, srcStr);
    return 1;
}
